<?php
if (file_exists ( dirname ( __FILE__ ) . '/ObjectAccess.class.php' )) {
	require_once dirname ( __FILE__ ) . '/ObjectAccess.class.php';
}

/**
 * Provides WordXMLDocument elements creation and manipulation
 * @author andrei.nemeti <ane@auximus.com>
 */
class WordXmlDocumentElement extends WordXmlDocument {
	/**
	 * The content of the template (XML)
	 * @var string
	 */
	private $_template = NULL;
	
	/**
	 * The content added to the element
	 * @var array
	 */
	private $_content = array ();
	
	/**
	 * The name of the element - will be used as varname in template 
	 * @var string
	 */
	private $_elementName = NULL;
	
	/**
	 * Prepare the element
	 * @param string $elementName
	 * @param string $templateDir
	 * @throws Exception
	 */
	public function __construct($elementName = NULL, $templateDir = NULL) {
		if (is_null ( $templateDir ) || ! file_exists ( $templateDir ) || ! is_dir ( $templateDir )) {
			throw new Exception ( 'Template directory is required in <i>' . __CLASS__ . '</i>' );
			return false;
		}
		$this->_elementName = $elementName;
		$this->_templateDir = $templateDir;
	}
	
	/**
	 * Load the template of the element, and if content is provided then add it
	 * @param string $elementName
	 * @param string $contents
	 * @return boolean
	 * @throws Exception
	 */
	public function load($elementName = NULL, $contents = NULL) {
		if (is_null ( $elementName )) {
			throw new Exception ( 'Element name is required in <i>' . __CLASS__ . '</i> method <i>' . __FUNCTION__ . '()</i>' );
			return false;
		}
		
		if (! file_exists ( $this->_templateDir . $elementName . '.xml' )) {
			throw new Exception ( 'Template directory <i>' . $this->_templateDir . '</i> does not contain a <i>' . $elementName . '.xml</i> file' );
			return false;
		}
		
		$this->_template = file_get_contents ( $this->_templateDir . $elementName . '.xml' );
		
		$this->addContent ( $contents, $elementName );
		
		return true;
	}
	
	/**
	 * Add content to an element &lt;varname /&gt;
	 * @param mixed $content String or WordXmlDocumentElement
	 * @param string $elementName If missing will use the WordXmlDocumentElement elementName
	 */
	public function addContent($content = NULL, $elementName = NULL) {
		if (is_null ( $elementName )) {
			$elementName = $this->_elementName;
		}
		
		if (! is_object ( $content )) {
			$this->_content [$elementName] [] = $this->_fixChars ( $content );
		} else {
			$this->_content [$elementName] [] = $content->output ();
		}
		return true;
	}
	
	/**
	 * Clear the contents of the element
	 * @param string $elementName Optional
	 */
	public function clean($elementName = NULL) {
		if (! is_null ( $elementName )) {
			$this->_content [$elementName] = array ();
		} else {
			$this->_content [] = array ();
		}
		return true;
	}
	
	/**
	 * Output the contents of the element
	 * @return string
	 */
	public function output() {
		$all_content = $this->_template;
		foreach ( $this->_content as $element => $contents ) {
			
			$element_content = '';
			foreach ( $contents as $content ) {
				$element_content .= $content;
			}
			$all_content = str_replace ( '<varname>' . strtoupper ( $element ) . '</varname>', $element_content, $all_content );
		}
		
		return $all_content;
	}
	
	/**
	 * Fix the contents of the XML tag
	 * @param string $string
	 * @throws Exception
	 * @todo Implement
	 */
	private function _fixChars($string = NULL) {
		if (is_null ( $string )) {
			$string = '';
		}
		return $string;
	}
	
	public function __destruct() {
	
	}
}